package cn.daliyTest.com.thread.processCommunication;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * lockSupport 灵活的实现线程通信的工具，只需要知道线程名字
 *
 * @author ：ljf
 * @date ：Created in 2021/6/24 11:42
 */
public class TestSyncByLockSupport {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        final Thread threadB = new Thread(()->{
            if(list.size()!=5){
                LockSupport.park();
            }
            System.out.println("b start");
        },"ThreadB");

        Thread threadA = new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                list.add("aaa");
                System.out.println("A added ,list:"+list.size());
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(list.size() ==5){
                    LockSupport.unpark(threadB);//? 怎么根据名字查询进程
                }
            }
        });
        threadA.start();
        threadB.start();
    }
}
